package com.toman.ui.work.management;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.toman.Enum.NetworkEnum;
import com.toman.Interfaces.NewBaseUrl;
import com.toman.JavaBean.FeedbackEntity;
import com.toman.R;
import com.toman.Util.DefaultAcitonCallback;
import com.toman.Util.DialogUtils;
import com.toman.Util.ProgressDialogUtils;
import com.toman.Util.ToastUtil;
import com.toman.Util.ViewUtil;
import com.toman.bean.ItemListBean;
import com.toman.camera.Camera;
import com.toman.toolbar.ToolBarActivity;
import com.toman.widget.ClearEditText;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class ProblemFeedbackProgressActivity extends ToolBarActivity {
    FeedbackEntity bean = new FeedbackEntity();
    Camera camera;
    @BindView(R.id.iv_more1)
    ImageView ivMore1;
    @BindView(R.id.rl_more1)
    RelativeLayout rlMore1;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.tv_company)
    TextView tvCompany;
    @BindView(R.id.rl_company)
    RelativeLayout rlCompany;
    @BindView(R.id.et_problem)
    ClearEditText etProblem;
    @BindView(R.id.tv_dep)
    TextView tvDep;
    @BindView(R.id.tv_problem_type)
    TextView tvProblemType;
    @BindView(R.id.tv_problem_object)
    TextView tvProblemObject;
    @BindView(R.id.tv_beltline)
    TextView tvBeltline;
    @BindView(R.id.rl_beltline)
    RelativeLayout rlBeltline;
    @BindView(R.id.tv_terminal)
    TextView tvTerminal;
    @BindView(R.id.rl_terminal)
    RelativeLayout rlTerminal;
    @BindView(R.id.et_remark)
    ClearEditText etRemark;
    @BindView(R.id.ll_more1)
    LinearLayout llMore1;
    @BindView(R.id.iv_more2)
    ImageView ivMore2;
    @BindView(R.id.rl_more2)
    RelativeLayout rlMore2;
    @BindView(R.id.tv_create_man)
    TextView tvCreateMan;
    @BindView(R.id.tv_create_time)
    TextView tvCreateTime;
    @BindView(R.id.ll_create)
    LinearLayout llCreate;
    @BindView(R.id.tv_edit_man)
    TextView tvEditMan;
    @BindView(R.id.tv_edit_time)
    TextView tvEditTime;
    @BindView(R.id.ll_edit)
    LinearLayout llEdit;
    @BindView(R.id.ll_more2)
    LinearLayout llMore2;
    @BindView(R.id.iv_more3)
    ImageView ivMore3;
    @BindView(R.id.rl_more3)
    RelativeLayout rlMore3;
    @BindView(R.id.tv_pre_progress_man)
    TextView tvPreProgressMan;
    @BindView(R.id.tv_pre_progress_time)
    TextView tvPreProgressTime;
    @BindView(R.id.ll_more3)
    LinearLayout llMore3;
    @BindView(R.id.ll)
    LinearLayout ll;
    @BindView(R.id.ll_feedback)
    LinearLayout llFeedback;
    @BindView(R.id.tv_distribution_man)
    TextView tvDistributionMan;
    @BindView(R.id.tv_distribution_time)
    TextView tvDistributionTime;
    @BindView(R.id.ll_distribution)
    LinearLayout llDistribution;
    @BindView(R.id.iv_more4)
    ImageView ivMore4;
    @BindView(R.id.rl_more4)
    RelativeLayout rlMore4;
    @BindView(R.id.et_handler)
    ClearEditText etHandler;
    @BindView(R.id.ll_more4)
    LinearLayout llMore4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_problem_feedback_progress);
        ButterKnife.bind(this);
        initToolBar("问题处理");
        initIntent();
        initView();
    }

    private void initView() {
        Object[] str = new Object[]{bean.getFeedbackNum(), bean.getCompanyName(), bean.getFeedbackName(), bean.getDepartmentName(), getResources().getStringArray(R.array.ProblemType)[Integer.parseInt(bean.getFeedbackType())],
                getResources().getStringArray(R.array.ProblemObject)[Integer.parseInt(bean.getFeedbackObject())], bean.getBeltlineName(), bean.getTerminalName(),
                bean.getFeedbackDescribe(), bean.getCreateUserName(), bean.getCreateTime(), bean.getUpdateUserName(), bean.getUpdateTime(), bean.getDistributionUserName(), bean.getDistributionTime(),
                bean.getHandleUserName(), bean.getEtc()};
        ViewUtil.setText(str, tvCode, tvCompany, etProblem, tvDep, tvProblemType, tvProblemObject, tvBeltline, tvTerminal, etRemark, tvCreateMan,
                tvCreateTime, tvEditMan, tvEditTime, tvDistributionMan, tvDistributionTime, tvPreProgressMan, tvPreProgressTime);
        if (bean.getFeedbackObject().equals("0")) {
            ViewUtil.visible(rlBeltline, rlTerminal);
        } else {
            ViewUtil.gone(rlBeltline, rlTerminal);
        }
        ViewUtil.unedit(etProblem, etRemark);
        String url[] = new String[bean.getProblemImageList().size()];
        for (int i = 0; i < url.length; i++) {
            url[i] = bean.getProblemImageList().get(i).getImagePath();
        }
        ViewUtil.createPhoto("问题图片", url, camera, this, llFeedback, true);
        camera = ViewUtil.createPhoto("请提供相关问题的截图或照片", camera, this, llMore4);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.toolbar_finish, menu);

        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.menu_finish:
                        add();
                        break;
                }
                return false;
            }
        });

        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        camera.onActivityResult(requestCode, resultCode, data);
    }

    private void initIntent() {
        bean = (FeedbackEntity) getIntent().getSerializableExtra("bean");
    }

    public void add() {
        String[] item = new String[]{"处理详情描述"};//直接初始化
        if (ViewUtil.isNull(getApplicationContext(), item, etHandler)) {
            return;
        }
        if (bean.getFeedbackObject().equals("产线终端")) {
            String[] item2 = new String[]{"产线名称", "终端名称"};//直接初始化
            if (ViewUtil.isNull(getApplicationContext(), item2, tvBeltline, tvTerminal)) {
                return;
            }
        }

        dialog = ProgressDialogUtils.showProgressDialog(this, "正在上传中", "请稍后");

        List<String> list = camera.getCompressUrl();
        List<File> files = new ArrayList<>();
        for (String path : list) {
            File file = new File(path);
            files.add(file);
        }
        OkHttpUtils
                .post(NewBaseUrl.management_feedback_dealwith)
                .headers("token", token)
                .headers("Content-Type", "multipart/form-data")
                .addFileParams("file", files)
                .params("id", bean.getId() + "")
                .params("deleteUrls", camera.getDeleteUrlNew())
                .params("feedbackStatus", bean.getFeedbackStatus())
                .params("processingDescribe", etHandler.getText().toString())
                .execute(new StringCallback() {
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                        Toast.makeText(getApplicationContext(), "请检查您的网络连接!", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }

                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", getClass().toString() + result);
                        JSONObject jsonObj = JSON.parseObject(result);
                        int code = jsonObj.getInteger("code");ViewUtil.lagout(code,getApplicationContext());

                        if (code == 0) {
                            ToastUtil.makeText(getApplicationContext(), jsonObj.getString("msg"));
                            dialog.dismiss();

                            finish();

                        } else {
                            ToastUtil.error(getApplicationContext(), jsonObj.getString("msg"));
                        }
                        dialog.dismiss();

                    }
                });

    }

    @OnClick({R.id.rl_more1, R.id.rl_more2, R.id.rl_more3, R.id.rl_more4})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.rl_more1:
                ViewUtil.hide(llMore1, ivMore1);
                break;
            case R.id.rl_more2:
                ViewUtil.hide(llMore2, ivMore2);
                break;
            case R.id.rl_more3:
                ViewUtil.hide(llMore3, ivMore3);
                break;
            case R.id.rl_more4:
                ViewUtil.hide(llMore4, ivMore4);
                break;

        }
    }
}